//
//  AccountModel.m
//  test
//
//  Created by fcn on 2020/8/13.
//  Copyright © 2020 hdj. All rights reserved.
//

#import "AccountModel.h"

@implementation AccountModel

- (void)setLogisticsCompany:(NSString *)logisticsCompany{
    _logisticsCompany = logisticsCompany;
    _isAgent = [logisticsCompany isEqualToString:@"中通快递"];
}

- (void)setProcedureRates:(double)procedureRates{
    _procedureRates = procedureRates;
    // 支付手续费用 == 支付手续费率 * 买家实际支付金额
    _poundageCost = _buyerActualPay * procedureRates;
}

+ (NSArray<AccountModel*> *)readOrderList{
    NSString* path = [[NSBundle mainBundle] pathForResource:order_list_template ofType:@"xlsx"];
//    NSLog(@"%@", path);
    
    NSURL* url = [NSURL URLWithString:path];
    
    MYCExcelWookbook* wb = [[MYCExcelWookbook alloc] initWithExcelFilePathUrl:url];
    MYCExcelSheet* sheet = wb.sheetArray.firstObject;
    
    NSMutableArray* dataArr = [NSMutableArray new];

    NSInteger currentRow = 0;
    for (MYCExcelCell* cell in sheet.cellArray) {
        NSMutableArray<MYCExcelCell *>* arr = [NSMutableArray new];
        if (cell.row == currentRow) {
            arr = dataArr.lastObject;
            [arr addObject:cell];
        }else{
            currentRow = cell.row;
            [dataArr addObject:arr];
            [arr addObject:cell];
        }
    }
    NSArray* res = [self cells2models:dataArr];
    return res;
}

+ (NSArray<AccountModel*>*)cells2models:(NSArray<NSMutableArray<MYCExcelCell*>*> *)dataArr{
    NSMutableArray<AccountModel*> * modelArr = [NSMutableArray new];
    NSMutableArray<NSString*> * attriArr = [NSMutableArray new];

    for (NSInteger i = 0; i < dataArr.count; i++) {
        NSArray<MYCExcelCell *> *arr = dataArr[i];
        if (i == 0) {
            // 第一行是标题
            for (MYCExcelCell* cell in arr) {
                // 创建属性名映射数组
                NSString* attriName = [self getAttriName:cell.stringValue];
                if (attriName) {
                    [attriArr addObject:attriName];
                }
            }
            if (attriArr.count != dataArr.firstObject.count) {
                NSLog(@"属性数量错误");
                return nil;
            }
        }else{
            AccountModel* model = [AccountModel new];
            for (NSInteger j = 0; j < arr.count; j++) {
                MYCExcelCell *cell = arr[j];
                NSString* attriName = attriArr[j];
                if (cell.stringValue) {
                    [model setValue:cell.stringValue forKey:attriName];
                }
            }
            [modelArr addObject:model];
        }
    }
    
    return modelArr;
}

+ (NSString*)getAttriName:(NSString*)name{
    if ([name isEqualToString:@"订单编号"]) {
        return @"orderNO";
    }else if ([name isEqualToString:@"买家会员名"]){
        return @"buyerName";
    }else if ([name isEqualToString:@"买家应付货款"]){
        return @"buyerPayable";
    }else if ([name isEqualToString:@"买家应付邮费"]){
        return @"needPayPostage";
    }else if ([name isEqualToString:@"总金额"]){
        return @"totalAmount";
    }else if ([name isEqualToString:@"买家实际支付金额"]){
        return @"buyerActualPay";
    }else if ([name isEqualToString:@"订单状态"]){
        return @"orderStatus";
    }else if ([name isEqualToString:@"订单创建时间"]){
        return @"orderCreateTime";
    }else if ([name isEqualToString:@"订单付款时间"]){
        return @"orderPayTime";
    }else if ([name isEqualToString:@"宝贝标题"]){
        return @"goodTitles";
    }else if ([name isEqualToString:@"宝贝种类"]){
        return @"goodKinds";
    }else if ([name isEqualToString:@"物流公司"]){
        return @"logisticsCompany";
    }else if ([name isEqualToString:@"宝贝总数量"]){
        return @"goodTotalQuantity";
    }else if ([name isEqualToString:@"订单关闭原因"]){
        return @"orderCloseReason";
    }else if ([name isEqualToString:@"退款金额"]){
        return @"refundAmount";
    }else if ([name isEqualToString:@"确认收货时间"]){
        return @"confirmReceiptTime";
    }else if ([name isEqualToString:@"打款商家金额"]){
        return @"finalPaymentAmount";
    }else if ([name isEqualToString:@"是否为刷单"]){
        return @"isAgent";
    }else if ([name isEqualToString:@"支付手续费率"]){
        return @"procedureRates";
    }else if ([name isEqualToString:@"支付手续费用"]){
        return @"poundageCost";
    }
    return nil;
}

@end
